package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysWorkingQueueMapper;
import com.settlement.system.model.entity.SysWorkingQueue;
import com.settlement.system.model.form.SysWorkingQueueForm;
import com.settlement.system.model.query.SysWorkingQueueQuery;
import com.settlement.system.model.vo.SysWorkingQueueImportVo;
import com.settlement.system.service.SysWorkingQueueService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作队列表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-30
 */
@Service
public class SysWorkingQueueServiceImpl extends ServiceImpl<SysWorkingQueueMapper, SysWorkingQueue> implements SysWorkingQueueService {

    @Override
    public Page<SysWorkingQueue> getPage(SysWorkingQueueQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysWorkingQueue> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysWorkingQueue(SysWorkingQueueForm sysWorkingQueueForm) {
        SysWorkingQueue sysWorkingQueue = new SysWorkingQueue();
        BeanUtils.copyProperties(sysWorkingQueueForm, sysWorkingQueue);
        return this.saveOrUpdate(sysWorkingQueue);
    }

    @Override
    public SysWorkingQueueForm getSysWorkingQueueFormData(Long id) {
        SysWorkingQueue sysWorkingQueue = this.getById(id);
        SysWorkingQueueForm sysWorkingQueueForm = new SysWorkingQueueForm();
        BeanUtils.copyProperties(sysWorkingQueue, sysWorkingQueueForm);
        return sysWorkingQueueForm;
    }

    @Override
    public boolean deleteSysWorkingQueue(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysWorkingQueueImportVo> voList) {
        List<SysWorkingQueue> sysWorkingQueueList = new ArrayList<>();
        for (SysWorkingQueueImportVo importVo : voList) {
            SysWorkingQueue sysWorkingQueue = new SysWorkingQueue();
            BeanUtils.copyProperties(importVo, sysWorkingQueue);
            sysWorkingQueueList.add(sysWorkingQueue);
        }
        this.saveOrUpdateBatch(sysWorkingQueueList);
    }

    @Override
    public List<SysWorkingQueueImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysWorkingQueue> sysWorkingQueueList = this.getBaseMapper().selectBatchIds(ids);
        List<SysWorkingQueueImportVo> voList = new ArrayList<>();
        for (SysWorkingQueue sysWorkingQueue : sysWorkingQueueList) {
            SysWorkingQueueImportVo vo = new SysWorkingQueueImportVo();
            BeanUtils.copyProperties(sysWorkingQueue, vo);
            voList.add(vo);
        }
        return voList;
    }
}
